#include "tpa626.h"
#include "myiic.h"
#include <rtthread.h>

uint16_t TPA626_Read2Byte(_myiic_t *iic, uint8_t reg_addr, uint8_t devAddr)
{
    uint8_t high, low;

    iic_start(iic);
    iic_send_byte(iic, I2C_WRITE(devAddr));
    if (iic_wait_ack(iic)) { iic_stop(iic); return 0; }

    iic_send_byte(iic, reg_addr);
    if (iic_wait_ack(iic)) { iic_stop(iic); return 0; }

    iic_start(iic);
    iic_send_byte(iic, I2C_READ(devAddr));
    if (iic_wait_ack(iic)) { iic_stop(iic); return 0; }

    high = iic_read_byte(iic, 1);
    low = iic_read_byte(iic, 0);
    iic_stop(iic);

    return ((uint16_t)high << 8) | low;
}

uint8_t TPA626_Write2Byte(_myiic_t *iic, uint8_t reg_addr, uint16_t value, uint8_t devAddr)
{
    iic_start(iic);
    iic_send_byte(iic, I2C_WRITE(devAddr));
    if (iic_wait_ack(iic)) { iic_stop(iic); return 0; }

    iic_send_byte(iic, reg_addr);
    if (iic_wait_ack(iic)) { iic_stop(iic); return 0; }

    iic_send_byte(iic, (value >> 8) & 0xFF);
    if (iic_wait_ack(iic)) { iic_stop(iic); return 0; }

    iic_send_byte(iic, value & 0xFF);
    if (iic_wait_ack(iic)) { iic_stop(iic); return 0; }

    iic_stop(iic);
    rt_thread_mdelay(2);
    return 1;
}



#include "tpa626.h"
#include "myiic.h"

extern _myiic_t at24c02_iic;

void tpa626_test(void)
{
    // 配置寄存器
    TPA626_Write2Byte(&at24c02_iic, Config_Reg, 0x4527, TPA626_ADDR_WR);
    TPA626_Write2Byte(&at24c02_iic, Calib_Reg, 0x0800, TPA626_ADDR_WR);

    // Manufacturer ID
    uint16_t man_id = TPA626_Read2Byte(&at24c02_iic, Man_ID_Reg, TPA626_ADDR_WR);
    rt_kprintf("VD1 Manufacturer ID: 0x%04X\n", man_id);

    // Device ID
    uint16_t id = TPA626_Read2Byte(&at24c02_iic, ID_Reg, TPA626_ADDR_WR);
    rt_kprintf("VD1 ID: 0x%04X\n", id);

    // Shunt Voltage
    uint16_t shunt = TPA626_Read2Byte(&at24c02_iic, Shunt_V_Reg, TPA626_ADDR_WR);
    rt_kprintf("VD1 Shunt_V_Reg: 0x%04X\n", shunt);
    float vshunt = shunt * 2.5e-6f;
    int vshunt_uV = (int)(vshunt * 1e6);
    rt_kprintf("Shunt Voltage: %d.%06d V\n", vshunt_uV / 1000000, abs(vshunt_uV % 1000000));

    // 计算电流
    float current = vshunt / 0.005f;
    int current_mA = (int)(current * 1000);
    rt_kprintf("Bus Current: %d.%03d A\n", current_mA / 1000, abs(current_mA % 1000));

    // Bus Voltage
    uint16_t voltage = TPA626_Read2Byte(&at24c02_iic, Bus_V_Reg, TPA626_ADDR_WR);
    rt_kprintf("VD1 Bus_V_Reg: 0x%04X\n", voltage);
    float vbus = voltage * 1.25e-3f; // 假设每 LSB = 1.25 mV
    int vbus_mV = (int)(vbus * 1000);
    rt_kprintf("Bus Voltage: %d.%03d V\n", vbus_mV / 1000, abs(vbus_mV % 1000));

    // Power
    // uint16_t power = TPA626_Read2Byte(&at24c02_iic, Power_Reg, TPA626_ADDR_WR);
    // rt_kprintf("VD1 Power_Reg: 0x%04X\n", power);
    // float pwr = power * 25e-3f; // 假设每 LSB = 25 mW
    // int pwr_mW = (int)(pwr * 1000);
    // rt_kprintf("Power: %d.%03d W\n", pwr_mW / 1000, abs(pwr_mW % 1000));

    // // Current Register (直接芯片计算值)
    // uint16_t cur = TPA626_Read2Byte(&at24c02_iic, Current_Reg, TPA626_ADDR_WR);
    // rt_kprintf("VD1 Current_Reg: 0x%04X\n", cur);
}

MSH_CMD_EXPORT(tpa626_test, TPA626 I2C test);

